import { defineStore } from 'pinia'
import { ref } from 'vue'
import { setTokenStorage, getTokenStorage, delTokenStorage, setUserInfoStorage, getUserInfoStorage, delUserInfoStorage } from '@/utils/storage'

export const useUserStore = defineStore('user-store', () => {
    // token
    const token = ref(getTokenStorage())
    // 存token
    const setToken = (newToken) => {
        token.value = newToken
        setTokenStorage(newToken)
    }
    // 删除token
    const delToken = () => {
        token.value = ''
        delTokenStorage()
    }

    // 用户基本信息
    const userInfo = ref(getUserInfoStorage())
    // 用户信息存本地
    const setUserInfo = (obj) => {
        userInfo.value = obj
        setUserInfoStorage(obj)
    }
    // 本地删除用户信息
    const delUserInfo = () => {
        userInfo.value = {}
        delUserInfoStorage()
    }

    return {
        token,
        userInfo,
        setToken,
        delToken,
        setUserInfo,
        delUserInfo
    }
})